* David Sturrock
* Jan 2018
* Creates inputs for annuitisation model *
* This is the alternative version which uses the unscaled life table survival probabilities *

	* setup *
clear all
set maxvar 30000
set more off


/**************************************************************************************************/
/*   Use UNSCALED life tables data to calculate fair annuity rates for each sex/age/birth year	  */
/**************************************************************************************************/

* this file is made in the "Fitted ELSA survival curves" do file 
clear
use "$temp\ons_survival.dta"


*** SET REAL INTEREST RATE ***

gen r = 0.0

*** Generate objective annuity rate ***

gen real_cost = 0
forv t = 0(1)60 {
gen prob_alive_in_`t' = 0
	forv age = 50(1)110 {
	replace prob_alive_in_`t' = prob`age' if `age' == age + `t'
	}
replace real_cost = real_cost + prob_alive_in_`t'/(1+r)^`t' 
}

gen annuity_rate = 1/real_cost

keep age dobyear sex annuity_rate

save "$temp\objective_annuity_rates_unscaled.dta", replace

/**************************************************************************************/
/*   Merge Weibull parameters and objective annuity rates							  */
/**************************************************************************************/

* uses files created in file "05 Make parameters.do"
clear
use "$temp\Weibull_parameters.dta"

merge 1:1 idauniq wave using "$savedata\wave1-7base.dta"
assert _merge != 1
keep if _m == 3
drop _m

merge m:1 age sex dobyear using "$temp\objective_annuity_rates_unscaled.dta"
assert _m != 1
keep if _m == 3
drop _m

keep idauniq dobyear wave sex age newk newinvlambda annuity_rate exlo80 exlo90 sample1 
rename newk k
rename newinvlambda lambda

sort wave idauniq 
gen id = _n
save "$temp\model_parameter_inputs_unscaled.dta", replace


//***********************************************
//
//	Rest of the files prepares inputs to model
//
//***********************************************


/**************************************************************************************/
/*  		 Merge in wealth and state pension income								  */
/**************************************************************************************/

clear
use "$temp\model_parameter_inputs_unscaled.dta"

* only have pension info up to wave 5 *
keep if inrange(wave,3,5)

* merge in pension wealth variables *
gen pension_wlth = . 
merge m:1 idauniq using "$penv3", keepusing(pripenw_spa)
drop if _m == 2
replace pension_wlth = pripenw_spa if wave == 3
drop _m
merge m:1 idauniq using "$penv4", keepusing(pripenw_spa) update replace 
drop if _m == 2
replace pension_wlth = pripenw_spa if wave == 4
drop _m
merge m:1 idauniq using "$penv5", keepusing(pripenw_spa) update replace
drop if _m == 2
replace pension_wlth = pripenw_spa if wave == 5
drop _m
drop pripenw_spa

* merge in financial wealth and state pension income variables *
gen fin_wlth = .
gen sp_inc = .
gen house = .
merge m:1 idauniq using "$findv3", keepusing(grossfw_bu_s spen_r_i hsval_hh_i)
drop if _m == 2
replace fin_wlth = grossfw_bu_s if wave == 3
replace sp_inc = spen_r_i if wave == 3
replace house = hsval_hh_i if wave  == 3
drop _m
merge m:1 idauniq using "$findv4", keepusing(grossfw_bu_s spen_r_i hsval_hh_i) update replace 
drop if _m == 2
replace fin_wlth = grossfw_bu_s if wave == 4
replace sp_inc = spen_r_i if wave == 4
replace house = hsval_hh_i if wave  == 4
drop _m
merge m:1 idauniq using "$findv5", keepusing(grossfw_bu_s spen_r_i hsval_hh_i) update replace
drop if _m == 2
replace fin_wlth = grossfw_bu_s if wave == 5
replace sp_inc = spen_r_i if wave == 5
replace house = hsval_hh_i if wave  == 5
drop _m
drop grossfw_bu_s
drop spen_r_i 

* merge in variable on whether deferred state pension from core data *
gen def_sp = .
merge m:1 idauniq using "$core4", keepusing(wpspd1b)
drop if _m == 2
replace def_sp = wpspd1b if wave == 4
drop _m
merge m:1 idauniq using "$core5", keepusing(wpspd1b) update replace 
drop if _m == 2
replace def_sp = wpspd1b if wave == 5
drop _m
drop wpspd1b def_sp

* merge in dob for women and calculate over/under SPA *
merge m:1 idauniq using "$dob"
* Keep only matched obsevations *
keep if _merge == 3
drop _merge
* calculate if over or under SPA *
gen d_birth = day(dob)
gen m_birth = month(dob)
gen y_birth = year(dob)
label var d_birth "Day of birth"
label var m_birth "Month of birth"
label var y_birth "Year of birth"
run "$dofiles\05.1 calc state pension age.do"

* merge in interview date *
merge m:1 idauniq using "$intdat"
assert _merge !=1
keep if _merge == 3 
drop _merge
* Put interview date into long form *
rename intdthw1 intdtiw1
gen intdtiw =.
label variable intdtiw "Interview Date"
	forvalues t = 1(1)8 {
	replace intdtiw = intdtiw`t' if wave == `t'
	drop intdtiw`t'
	format intdtiw %d
	}
* save interview dates for later use *
preserve
keep idauniq wave intdtiw
save "$temp\int_dates.dta", replace
restore

gen over_SPA = .
label var over_SPA "Over SPA at date of interview"
replace over_SPA = 1 if spad <= intdtiw & spad !=. & intdtiw !=.
replace over_SPA = 0 if spad > intdtiw & spad !=. & intdtiw !=.
drop spad* *_birth

* merge in whether have partner *
gen partner = . 
merge m:1 idauniq using "$dv3", keepusing(idauniq_p)
drop if _m == 2
replace partner = idauniq_p if wave == 3
drop _m
merge m:1 idauniq using "$dv4", keepusing(idauniq_p) update replace 
drop if _m == 2
replace partner = idauniq_p if wave == 4
drop _m
merge m:1 idauniq using "$dv5", keepusing(idauniq_p) update replace
drop if _m == 2
replace partner = idauniq_p if wave == 4
drop _m
drop idauniq_p

gen has_partner = idauniq != .

* create total wealth variable (to be annuitised)
gen assets = fin_wlth + pension_wlth
replace assets = assets/2 if has_partner

* make year of interview var *
gen y_int = year(intdtiw)

* put in pension credit guarantee level as minimum - levels from IFS tax and benefits survey*
gen pen_cred = .
replace pen_cred = 114.05 if y_int == 2006
replace pen_cred = 119.05 if y_int == 2007
replace pen_cred = 124.05 if y_int == 2008
replace pen_cred = 130.00 if y_int == 2009
replace pen_cred = 132.60 if y_int == 2010
replace pen_cred = 137.35 if y_int == 2011
order pen_cred, last

* make state pension and pension credit income an annual amount *
replace sp_inc = sp_inc *52
replace pen_cred = pen_cred*52

* generate housing consumption *
gen house_inc = house*0.04
replace house_inc = 0 if house == .

* Select sample *
keep if over_SPA
gen says_50 = exlo80 == 50 | exlo90 == 50
drop if sp_inc <=0
keep if assets > 0 & assets != .
* keep if sample1 // sample selection that drops those who answered 100
drop over_SPA intdtiw fin_wlth pension_wlth y_int has_partner partner house hsval_hh_i exlo80 exlo90 sample1 
order idauniq, last

* 2 people aged 59 - impossible *
drop if age < 60

save "$temp\model_inputs_unscaled.dta", replace
drop wave sex
outsheet using "$savedata\model_inputs_subj_unscaled.csv" , comma nonames replace


/**************************************************************************************/
/*  		 Set up life table probs for 'accurate' expectations				  */
/**************************************************************************************/

/*************************************************************************/
/* Step 1: Make hazards													 */
/*************************************************************************/

clear
use "$temp\ons_survival.dta"

* generate vars for prob of death at specific age *

forv a = 50(1)109 {
	local b = `a' + 1
	gen hazard_`a' = 1- prob`b'/prob`a'
	replace hazard_`a' = . if age < 50 | prob110 == .
	}
gen hazard_110 = 1
replace hazard_110 = . if age < 50 | prob110 == .

keep age dobyear sex hazard* prob*

save "$temp\hazards_by_age_unscaled.dta", replace


/*************************************************************************/
/* Step 2: Merge in with file on individuals' pension wealth etc	  	 */
/*************************************************************************/


clear
use "$temp\model_inputs_unscaled.dta"

* merge in death probs *
merge m:1 age sex dobyear using "$temp\hazards_by_age_unscaled.dta"
keep if _m == 3
drop _m
drop hazard_5* prob5*

* change from hazard and survival at particular age to hazard and survival in years time *
gen haz_fill = 1
* create hazards *
forv i = 0(1)51 {
	gen haz_`i' = .
	}

forv a  = 60(1)110 {
	forv t = 0(1)51 {
	replace haz_`t' = hazard_`a' if `a' == age + `t'
	replace haz_`t' = 1 if age + `t' > 110
	}
}

* create survival probs *

forv i = 0(1)51 {
	gen surv_`i' = .
	}
forv a  = 60(1)110 {
	forv t = 0(1)51 {
	local age = `a' - `t'
	if `a' - `t' >= 60 {
		replace surv_`t' = prob`a'/prob`age' if age == `age'
		}
	}
}

drop hazard* prob* surv* 
order house_inc pen_cred, last
sort idauniq wave
replace id = _n

drop says_50

* keep only the first observation of each person *
bys idauniq (wave) : gen obs = _n 
keep if obs == 1
drop obs

sort idauniq wave
replace id = _n

* save version with all *
sort idauniq wave
drop wave sex dobyear idauniq
export delimited using "$fortran\fortran_inputs_unscaled.txt" , delim(tab) novarnames replace
save "$savedata\fortran_inputs_unscaled.dta", replace

